let callbacks = [];
let pending = false;

function flushCallbacks() {
    pending = false; //把标志还原为false
    // 依次执行回调
    for (let i = 0; i < callbacks.length; i++) {
        callbacks[i]();
    }
}
let timerFunc; //定义异步方法  采用优雅降级
// 如果支持promise
if (typeof Promise !== "undefined") {
    // 利用promise进行微任务执行
    const p = Promise.resolve();
    timerFunc = () => {
        p.then(flushCallbacks);
    };
} else if (typeof MutationObserver !== "undefined") {
    // MutationObserver 主要是监听dom变化 也是一个异步方法
    let counter = 1;
    const observer = new MutationObserver(flushCallbacks);
    const textNode = document.createTextNode(String(counter));
    observer.observe(textNode, {
        characterData: true,
    });
    timerFunc = () => {
        counter = (counter + 1) % 2;
        textNode.data = String(counter);
    };
} else if (typeof setImmediate !== "undefined") {
    // 如果前面都不支持 判断setImmediate
    timerFunc = () => {
        setImmediate(flushCallbacks);
    };
} else {
    // 最后降级采用setTimeout
    timerFunc = () => {
        setTimeout(flushCallbacks, 0);
    };
}

// 1 调用nextTick
export function nextTick(cb) {
    // 除了渲染watcher  还有用户自己手动调用的nextTick 一起被收集到数组
    callbacks.push(cb);
    if (!pending) {
        // 如果多次调用nextTick  只会执行一次异步 等异步队列清空之后再把标志变为false
        pending = true;
        timerFunc();
    }
}